package com.wjd.gulimall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wjd.common.utils.PageUtils;
import com.wjd.common.utils.Query;
import com.wjd.gulimall.product.dao.CategoryBrandRelationDao;
import com.wjd.gulimall.product.entity.BrandEntity;
import com.wjd.gulimall.product.entity.CategoryBrandRelationEntity;
import com.wjd.gulimall.product.entity.CategoryEntity;
import com.wjd.gulimall.product.service.BrandService;
import com.wjd.gulimall.product.service.CategoryBrandRelationService;
import com.wjd.gulimall.product.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;
import java.util.Map;

@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private BrandService brandService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String brandId = (String) params.get("brandId");
        LambdaQueryWrapper<CategoryBrandRelationEntity> query = new LambdaQueryWrapper<CategoryBrandRelationEntity>()
                .eq(brandId != null, CategoryBrandRelationEntity::getBrandId, brandId);
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params), query
        );

        return new PageUtils(page);
    }

    @Override
    public List<CategoryBrandRelationEntity> catelogList(Map<String, Object> params) {
        String brandId = (String) params.get("brandId");
        LambdaQueryWrapper<CategoryBrandRelationEntity> query = new LambdaQueryWrapper<CategoryBrandRelationEntity>()
                .eq(brandId != null, CategoryBrandRelationEntity::getBrandId, brandId);
        List<CategoryBrandRelationEntity> list = this.list(query);
        if (list == null || list.isEmpty()) {
            return Collections.emptyList();
        }
        return list;
    }

    @Override
    public void saveRelation(CategoryBrandRelationEntity categoryBrandRelation) {
        // 1. 查询品牌名称
        BrandEntity brandEntity = brandService.getById(categoryBrandRelation.getBrandId());
        if (brandEntity == null) {
            throw new IllegalArgumentException("Brand not found for ID: " + categoryBrandRelation.getBrandId());
        }
        categoryBrandRelation.setBrandName(brandEntity.getName());
        // 2. 查询分类名称
        CategoryEntity categoryEntity = categoryService.getById(categoryBrandRelation.getCatelogId());
        if (categoryEntity == null) {
            throw new IllegalArgumentException("Category not found for ID: " + categoryBrandRelation.getCatelogId());
        }
        categoryBrandRelation.setCatelogName(categoryEntity.getName());
        // 3. 保存关联关系
        this.save(categoryBrandRelation);
    }

    @Override
    public void updateBrand(Long brandId, String name) {
        LambdaUpdateWrapper<CategoryBrandRelationEntity> updater = new LambdaUpdateWrapper<CategoryBrandRelationEntity>()
                .eq(CategoryBrandRelationEntity::getBrandId, brandId)
                .set(CategoryBrandRelationEntity::getBrandName, name);
        this.update(updater);
    }

    @Override
    public void updateCategory(Long catId, String name) {
        LambdaUpdateWrapper<CategoryBrandRelationEntity> updater = new LambdaUpdateWrapper<CategoryBrandRelationEntity>()
                .eq(CategoryBrandRelationEntity::getCatelogId, catId)
                .set(CategoryBrandRelationEntity::getCatelogName, name);
        this.update(updater);
    }

}